home *** CD-ROM | disk | FTP | other *** search
/ Amiga Collections: Memphis Amiga Group / MAG Disk (1990-09)(Memphis Amiga Group).zip / MAG Disk (1990-09)(Memphis Amiga Group).adf / sMOVIE / source / sMOVIE.c < prev    next >
C/C++ Source or Header  |  1989-12-29  |  13KB  |  619 lines

  1. /*    sMOVIE.c    by:-    Martin J. Round.
  2.                         17 Naseby Drive,
  3.                         Halesowen,
  4.                         West Mids.
  5.                         B63 1HJ
  6.                         England.
  7.                         
  8.                         Tel: (voice) 021-550-0014
  9.                         
  10.                         or contact me (Martin Round)
  11.                         on the Plug 'Ole BB:  021-472-0256
  12.                         
  13.                         16-Oct-89
  14. */
  15.  
  16. #include "sMOVIE.h"
  17.  
  18. /*    Declarations for CBACK    */
  19. extern BPTR _Backstdout;        /*    std output when run in background    */
  20. long _BackGroundIO = 1;            /*    Flag to tell it we want to do I/O    */
  21. long _stack = 4000;                /*    Amount of stack space task needs    */
  22. char *_procname = "sMOVIE";        /*    The name of the task to create        */
  23. long _priority = 1;                /*    The priority to run us at            */
  24.  
  25. struct NewWindow HelloWindow = {
  26.  
  27.     220,85,200,30,         /* position and size                        */
  28.     2,3,                  /* pens                                     */
  29.     NULL,                /* IDCMPFlags                                */
  30.     ACTIVATE |
  31.     NOCAREREFRESH,        /* flags                                    */
  32.     NULL,                /* no special gadgets                        */
  33.     NULL,                /* no special checkmark                     */
  34.     "  sMOVIE",
  35.     NULL,                /* pointer to screen                        */
  36.     NULL,                /* no bitmap                                */
  37.     0,0,                /* minimum sizes                            */
  38.     200,30,             /* maximum sizes                            */
  39.     WBENCHSCREEN,        /* type of screen                            */
  40.     };
  41.  
  42. struct Window *window, *OpenWindow();
  43.  
  44. /*    If priority is the standard 0 then when run from the CLI, the command
  45.     line prompt is put up before we can output our banner.
  46.     This looks messy - hence priority of 1.
  47. */
  48.  
  49. struct GfxBase *GfxBase;
  50. struct IntuitionBase *IntuitionBase;
  51.  
  52. struct DosLibrary *DosBase;
  53. struct Library *DiskfontBase;
  54.  
  55. struct TextFont *tf[MAXFONTS+1], *font, *defaultfont;
  56.  
  57. struct TextAttr ta;
  58.  
  59. struct View *oldview;    /* for saving the original view.        */
  60. struct View v;
  61. struct RasInfo ri;
  62. struct ViewPort vp;
  63. struct BitMap b;
  64. struct RastPort rp;     /* the stuctures for the view to be created */
  65.  
  66. extern struct ColorMap *GetColorMap();
  67.  
  68. extern void argread (int, char **, int);
  69.  
  70. extern void smoothtext (char *, short, short, BYTE, BYTE, struct TextFont *);
  71.  
  72. extern void smoothlines (short, short, short, BYTE, BYTE,);
  73.  
  74. extern int mousemonitor (void);
  75.  
  76. extern int initmousemonitor (void);
  77.  
  78. extern void removemousemonitor (void);
  79.  
  80. extern void message (UBYTE *);
  81.  
  82. extern void print (char *);
  83.  
  84. extern int Min (int, int);
  85.  
  86. extern void free_memory (int);
  87.  
  88. extern void on_sprite (void);
  89.  
  90. extern void off_sprite (void);
  91.  
  92. void resize (short,short,short,short,short);
  93.  
  94. char *txptr, *txstart,fontname[MAXFONTS][MAXFONTNAMELENGTH];
  95.  
  96. char line[MAXLINE + 1],buff[MAXLINE * 2];
  97.  
  98. int pointsize[MAXFONTS],fonts,textsiz,cmdsiz,maxfontheight,bitmapheight;
  99.  
  100. int height,width,maxheight,maxwidth,maxdepth,centring,ind,tab,ypos,delay;
  101.  
  102. int no_text_or_lines,normal;
  103.  
  104. BYTE apen,bpen;
  105.  
  106. short *cmdptr,*cmdstart;
  107.  
  108. char esc;
  109.  
  110. /*    following stuff is needed by mousemonitor()    */
  111.  
  112. int abort_prog;
  113.  
  114. extern MOUSE_INFO mouseinfo;
  115.  
  116. /*    following stuff so we can work from CLI or WorkBench    */
  117.  
  118. int runningfromcli;
  119.  
  120. #define MAXARGS 32
  121.  
  122. void _main(cmd)  /*    Using _main() and cback.o avoids the stupid console
  123.                     window opening when we are run from the Workbench    */
  124. char *cmd;
  125.  
  126. {
  127.     int argc = 0;
  128.     char *argv[MAXARGS];
  129.  
  130.     int i,j;
  131.     short spacing;
  132.  
  133.     /*    This code fragment gets argc and argv as if from main()        */
  134.     if (cmd && *cmd) {
  135.         argv[argc] = strtok(cmd,", \n");
  136.         while(argv[argc])
  137.             argv[++argc] = strtok(NULL,", \n");
  138.         }
  139.  
  140.     runningfromcli = (argc == 0 ? 0 : 1);
  141.     
  142.     window = NULL;
  143.  
  144.     if (runningfromcli) {    /*    Output my banner.    */
  145.         print("\n\2330;33msMOVIE 1.0\2330m  by\t\2331mMartin Round.\2330m\n");
  146.         print("\t\t17, Naseby Drive, Halesowen, West Mids.\n");
  147.         print("\t\tB63 1HJ  England.  Contact me through\n");
  148.         print("\t\tthe Plug 'Ole B.B.  (021-472-0256).\n\n");
  149.         }
  150.     
  151.     
  152.     if((IntuitionBase = (struct IntuitionBase *)
  153.         OpenLibrary("intuition.library",0)) == NULL) {
  154.             exit(0L);
  155.             }
  156.  
  157.     if ((DosBase = (struct DosLibrary *)
  158.         OpenLibrary("dos.library",0)) == NULL) {
  159.             message("Couldn't open dos.library'.");
  160.             free_memory(1);
  161.             exit(0L);
  162.             }
  163.  
  164.     if ((GfxBase = (struct GfxBase *)
  165.         OpenLibrary("graphics.library",0)) == NULL) {
  166.             message("Couldn't open graphics.library'.");
  167.             free_memory(2);
  168.             exit(0L);
  169.             }
  170.  
  171.     if (!runningfromcli) {    /*    Give Workbenchers something to see.    */
  172.         if ((window = OpenWindow(&HelloWindow)) == NULL) {
  173.             message("Couldn't open Window.");
  174.             free_memory(3);
  175.             exit(0);
  176.             }
  177.  
  178.         SetAPen(window->RPort,3);
  179.         SetDrMd(window->RPort,JAM1);
  180.         RectFill(window->RPort,2,11,197,28);
  181.         SetAPen(window->RPort,2);
  182.         Move(window->RPort,35,23);
  183.         Text(window->RPort,"Patience...",11);
  184.         }
  185.  
  186.     /*  open default font    */
  187.     ta.ta_Style = 0;
  188.     ta.ta_Flags = FPF_ROMFONT;
  189.     ta.ta_Name = DEFAULTFONTNAME;
  190.     ta.ta_YSize = DEFAULTFONTHEIGHT;
  191.  
  192.     if ((defaultfont = (struct TextFont *) OpenFont(&ta)) == NULL) {
  193.         message("Couldn't open default font.");
  194.         free_memory(3);
  195.         exit(0L);
  196.         }
  197.  
  198.     tf[0] = defaultfont;
  199.     
  200.     fonts = textsiz = cmdsiz = 0;
  201.  
  202.     height = maxheight = 0;
  203.  
  204.     maxdepth = 1;
  205.  
  206.     maxwidth = 0;
  207.     
  208.     /*  need this in case user tries to centre without first sizing    */
  209.     width = GfxBase->NormalDisplayColumns;
  210.     
  211.     centring = 0;
  212.  
  213.     tab = DEFAULTTABSIZE;
  214.     
  215.     ind = 0;
  216.  
  217.     font = defaultfont;
  218.  
  219.     maxfontheight = defaultfont->tf_YSize;
  220.  
  221.     esc = DEFAULT_ESC_CHAR;    /*    set defaults before 1st pass        */
  222.     
  223.     no_text_or_lines = 1;    /*    this gets cleared if any are found    */
  224.     
  225.     argread(argc, argv, 0);        /*    read file(s) for 1st time        */
  226.     
  227.     if (runningfromcli) {
  228.         if (fonts) {
  229.             print("FONTS REQUIRED:-\n");
  230.             for(i=0; i<fonts; i++) {
  231.                 sprintf(buff,"%2d:%16s %d\n",
  232.                     i+1, fontname[i], pointsize[i]);
  233.                 print(buff);
  234.                 }
  235.             print ("\n");
  236.             }
  237.         }
  238.         
  239.     if (fonts) {
  240.         if((DiskfontBase = (struct Library *)
  241.             OpenLibrary("diskfont.library",0)) == NULL) {
  242.                 message("Couldn't open 'diskfont.library'.");
  243.                 free_memory(4);
  244.                 exit(0L);
  245.                 }
  246.         }
  247.     else
  248.         DiskfontBase = NULL;
  249.  
  250.     ta.ta_Style = 0;
  251.     ta.ta_Flags = FPF_ROMFONT | FPF_DISKFONT;
  252.  
  253.     for (i=0; i<fonts; i++) {
  254.         strcpy(line,fontname[i]);
  255.         strcat(line,".font");
  256.         ta.ta_Name = line;
  257.         ta.ta_YSize = pointsize[i];
  258.  
  259.         if ((tf[i] = (struct TextFont *)OpenDiskFont(&ta)) == NULL) {
  260.             sprintf(buff,"Can't open Font: %20s %2d  Using default.",
  261.                 ta.ta_Name, ta.ta_YSize);    
  262.             message(buff);
  263.             tf[i] = defaultfont;
  264.             }
  265.  
  266.         if (tf[i]->tf_YSize > maxfontheight)
  267.             maxfontheight = tf[i]->tf_YSize;
  268.  
  269.         }
  270.  
  271.     if (no_text_or_lines) {
  272.         message("Nothing to display!");
  273.         free_memory(5);
  274.         exit(0L);
  275.         }
  276.  
  277.     if (textsiz == 0)
  278.         textsiz = 1;    /*  this kludge allows scripts without text
  279.                             to be run (otherwise getmem returns NULL)    */
  280.  
  281.     if ((txstart = getmem(textsiz)) == NULL) {
  282.         message("Couldn't get memory for text.");
  283.         free_memory(5);
  284.         exit(0L);
  285.         }
  286.  
  287.     if ((cmdstart =
  288.         (short *)getmem(cmdsiz * sizeof(short))) == NULL) {
  289.         message("Couldn't get memory for commands.");
  290.         free_memory(6);
  291.         exit(0L);
  292.         }
  293.  
  294.     /*    get a rastport for centring up text    */
  295.     InitBitMap(&b,1,640,50);
  296.     b.Planes[0] = (PLANEPTR)AllocRaster(640,50);
  297.     if(b.Planes[0] == NULL) {
  298.         message("Couldn't get centring bitmap.");
  299.         free_memory(7);
  300.         exit(0L);
  301.         }
  302.     InitRastPort(&rp);
  303.     rp.BitMap = &b;
  304.  
  305.     /*  pass 2  */
  306.  
  307.     centring = 0;
  308.  
  309.     tab = DEFAULTTABSIZE;
  310.     
  311.     ind = 0;
  312.  
  313.     font = defaultfont;
  314.  
  315.     esc = DEFAULT_ESC_CHAR;    /*    reset defaults before 2nd pass    */
  316.  
  317.     cmdptr = cmdstart;
  318.     txptr = txstart;
  319.  
  320.     argread(argc, argv, 1);        /*    read file(s) a 2nd (and final) time    */
  321.  
  322.     FreeRaster(b.Planes[0],640,50);
  323.     
  324.     if(!runningfromcli)
  325.         CloseWindow(window);
  326.         
  327.     window = NULL;
  328.  
  329. /*
  330. {
  331.     short *p;
  332.     int i;
  333.     for (p=cmdstart,i=0; i<cmdsiz; i++) {
  334.         sprintf(buff,"%4d",(int) *p++);
  335.         print(buff);
  336.         }
  337.         
  338. }
  339. */
  340.     if (maxheight < MIN_HEIGHT)
  341.         maxheight = GfxBase->NormalDisplayRows;
  342.  
  343.     if (maxwidth < MIN_WIDTH)
  344.         maxwidth = GfxBase->NormalDisplayColumns;
  345.  
  346.     bitmapheight = 2 * maxheight + 3 * (maxfontheight - 1) + 6;
  347.  
  348.     oldview = GfxBase->ActiView; /* save current view to restore later */
  349.  
  350.     InitView(&v);        /* initialize view */
  351.     InitVPort(&vp);     /* initialise view port */
  352.     v.ViewPort = &vp;    /* link view into viewport */
  353.  
  354.     /* init bit maps (for rasinfo ) */
  355.     InitBitMap(&b,maxdepth,maxwidth,bitmapheight);
  356.  
  357.     /* init RasInfo */
  358.     ri.BitMap = &b;
  359.     ri.RxOffset = 0;    
  360.     ri.RyOffset = 0;
  361.     ri.Next = NULL;
  362.  
  363.     /* init ViewPort */
  364.     vp.DWidth = Min(GfxBase->NormalDisplayColumns,maxwidth);
  365.     vp.DHeight = Min(GfxBase->NormalDisplayRows,maxheight);
  366.     vp.RasInfo = &ri;
  367.     vp.Modes = HIRES;
  368.     if((vp.ColorMap = GetColorMap(2 << maxdepth)) == NULL) {
  369.         message("Couldn't get memory for ColourMap.");
  370.         free_memory(8);
  371.         exit(0L);
  372.         }
  373.  
  374.     /* allocate space for bitmap */
  375.     for(i=0; i<maxdepth; i++) {
  376.         b.Planes[i] = (PLANEPTR)AllocRaster(maxwidth,bitmapheight);
  377.         if(b.Planes[i] == NULL) {
  378.             message("Couldn't get memory for bitmap.");
  379.             maxdepth = i;
  380.             free_memory(10);
  381.             exit(0L);
  382.             }
  383.         }
  384.  
  385.     /* open a RastPort to alow use of text and drawing routines */
  386.     InitRastPort(&rp);
  387.     rp.BitMap = &b;
  388.  
  389.     SetRast(&rp,0); /* clear the bitmap */
  390.     
  391.     if (initmousemonitor()) {
  392.         message("Couldn't initialise mouse monitor.");
  393.         free_memory(10);
  394.         exit(0L);
  395.         }
  396.         
  397.     abort_prog = 0;
  398.  
  399.     width = vp.DWidth;
  400.     height = vp.DHeight;
  401.     txptr = txstart;
  402.     cmdptr = cmdstart;
  403.     ypos = height + 6;
  404.     apen = 1;
  405.     bpen = 0;
  406.     SetRGB4(&vp,0,DEFAULTBACKGROUNDCOLOUR);
  407.     SetRGB4(&vp,1,DEFAULTPENCOLOUR);
  408.     font = defaultfont;
  409.     delay = DEFAULTDELAY;
  410.     spacing = DEFAULTSPACING;
  411.     normal = 1;                    /*    causes 'press mouse button' message    */
  412.  
  413.     SetTaskPri(FindTask(NULL),21);    /*    now run at high priority        */
  414.     
  415.     WaitTOF();
  416.     
  417.     off_sprite();    /*    kill intuition cursor    */
  418.  
  419.     while (!abort_prog && cmdptr - cmdstart < cmdsiz) {
  420.         switch (*cmdptr++) {
  421.  
  422.             case 1:     /*  text            */
  423.                 smoothtext(txptr,*cmdptr++,*cmdptr++,apen,bpen,font);
  424.                 smoothlines(spacing,0,0,bpen,bpen);
  425.                 txptr += *(cmdptr-2);
  426.                 continue;
  427.  
  428.             case 2:     /*  lines            */
  429.                 smoothlines(*cmdptr++,*cmdptr++,*cmdptr++,apen,bpen);
  430.                 continue;
  431.  
  432.             case 3:     /*  pen colours     */
  433.                 apen = *cmdptr++;
  434.                 bpen = *cmdptr++;
  435.                 break;
  436.  
  437.             case 4:     /*  change font     */
  438.                 font = tf[*cmdptr++];
  439.                 break;
  440.  
  441.             case 5:     /*  delay            */
  442.                 for (j=0; j< *cmdptr; j++) {
  443.                     if (abort_prog = mousemonitor()) break;
  444.                     WaitTOF();
  445.                     }
  446.                 ++cmdptr;
  447.                 continue;
  448.  
  449.             case 6:     /*  scroll speed    */
  450.                 delay = *cmdptr++;
  451.                 if (delay)
  452.                     SetTaskPri(FindTask(NULL),21);
  453.                 else
  454.                     SetTaskPri(FindTask(NULL),20);
  455.  
  456.                 break;
  457.  
  458.             case 7:     /*  clr             */
  459.                 SetRast(&rp,bpen);
  460.                 ri.RyOffset = 0;
  461.                 ypos = height + 6;
  462.                 MakeVPort(&v, v.ViewPort);
  463.                 MrgCop(&v);
  464.                 LoadView(&v);
  465.                 break;
  466.  
  467.             case 9:     /*  change spacing  */
  468.                 spacing = *cmdptr++;
  469.                 break;
  470.  
  471.             case 10:    /*  change pallette */
  472.                 WaitBOVP(&vp);
  473.                 SetRGB4(&vp,*cmdptr++,*cmdptr++,*cmdptr++,*cmdptr++);
  474.                 break;
  475.  
  476.             case 11:    /*    change size        */
  477.                 vp.Modes = 0;
  478.                 if (*cmdptr++)
  479.                     vp.Modes = HIRES;
  480.  
  481.                 resize
  482.                     (*cmdptr++,*cmdptr++,*cmdptr++,*cmdptr++,*cmdptr++);
  483.  
  484.                 break;
  485.  
  486.             case 12:    /*    smooth sizing      */
  487.                 {
  488.                 short ow,oh,ox,oy,oxo,w,h,x,y,xo;
  489.                 
  490.                 int delay;
  491.                 
  492.                 register int i,steps,old;
  493.                 
  494.                 ow = vp.DWidth;
  495.                 oh = vp.DHeight;
  496.                 ox = vp.DxOffset;
  497.                 oy = vp.DyOffset;
  498.                 oxo = ri.RxOffset;
  499.                 
  500.                 steps = *cmdptr++;
  501.                 delay = *cmdptr++;
  502.                 w = *cmdptr++;
  503.                 h = *cmdptr++;
  504.                 x = *cmdptr++;
  505.                 y = *cmdptr++;
  506.                 xo = *cmdptr++;
  507.                 
  508.                 for (i=1; i<=steps; i++) {
  509.                     old = steps-i;
  510.                     resize(    (short) ((ow  * old + w  * i)/steps),
  511.                             (short) ((oh  * old + h  * i)/steps),
  512.                             (short) ((ox  * old + x  * i)/steps),
  513.                             (short) ((oy  * old + y  * i)/steps),
  514.                             (short) ((oxo * old + xo * i)/steps) );
  515.                     if (abort_prog = mousemonitor()) break;
  516.                     for (j=0; j< delay; j++) {
  517.                         if (abort_prog = mousemonitor()) break;
  518.                         WaitTOF();
  519.                         }
  520.                     }
  521.                 }
  522.                 
  523.                 break;
  524.  
  525.             case 13:    /*    default sizes    */
  526.                 vp.Modes = HIRES;
  527.                 resize(
  528.                     GfxBase->NormalDisplayColumns,GfxBase->NormalDisplayRows,
  529.                     0,0,0);
  530.                     
  531.                 break;
  532.             
  533.             case 14:    /*    restart            */
  534.                 txptr = txstart;
  535.                 cmdptr = cmdstart;
  536.                 
  537.                 continue;
  538.  
  539.             default:
  540.                 sprintf(buff,"Error %d\n",*(cmdptr-1));
  541.                 print(buff);
  542.                 mouseinfo.button = -1;    /*    force abort    */
  543.                 break;
  544.  
  545.             }    /*  switch  */
  546.             
  547.         abort_prog = mousemonitor();
  548.         
  549.         }    /*while */
  550.  
  551.     if (!abort_prog  && normal) {
  552.         delay = 0;
  553.         smoothlines(2,0,0,0,0);
  554.         smoothlines(2,0,0,1,1);
  555.         smoothlines(2,0,0,0,0);
  556.         smoothtext("Press either Mouse button.",26,0,1,0,defaultfont);
  557.         smoothlines(5,0,0,0,0);
  558.         for(j=0; j<50; j++)
  559.             WaitTOF();
  560.         mouseinfo.button = 0;    /*    stops accidental pausing at end    */
  561.         while (!mouseinfo.button)
  562.             WaitTOF();
  563.         }
  564.  
  565.     SetTaskPri(FindTask(NULL),0);    /*    give other tasks a chance!        */
  566.     
  567.     on_sprite();                /*    come back pointer - we need you!    */
  568.  
  569.     LoadView(oldview);          /* put back the old view                */
  570.     
  571.     removemousemonitor();
  572.  
  573.     free_memory(10);
  574.     
  575.     exit(0L);
  576.  
  577. }    /*  end of main */
  578.  
  579. void resize(w,h,x,y,xo)
  580.  
  581. short w;    /*    width    */
  582. short h;    /*    height    */
  583. short x;    /*    x posn of view on screen    */
  584. short y;    /*    y posn of view on screen    */
  585. short xo;    /*    x offset into bitmap for view    */
  586. {
  587.     vp.DWidth = width = w;
  588.  
  589.     height = h;
  590.  
  591.     if (vp.DHeight >= height)    /*    getting smaller    or same    */
  592.         ypos -= (vp.DHeight - height);
  593.  
  594.     else if (ri.RyOffset >= (height - vp.DHeight))
  595.         ri.RyOffset = ri.RyOffset - height + vp.DHeight;
  596.  
  597.     else {    /*    must clear    */
  598.         ypos = height + 6;
  599.         ri.RyOffset = 0;
  600.         SetRast(&rp,bpen);
  601.         }
  602.  
  603.     vp.DHeight = height;
  604.     vp.DxOffset = x;
  605.     vp.DyOffset = y;                /*  *****NOTE*****  */
  606.                                     /*  Value in range  */
  607.                                     /*  11 - 48 causes  */
  608.                                     /*  \F/L\I/C\K/E\R  */
  609.                                     /*      WHY???      */
  610.  
  611.     ri.RxOffset = xo;
  612.     MakeVPort(&v, v.ViewPort);
  613.     MrgCop(&v);
  614.     WaitTOF();
  615.     LoadView(&v);
  616.     normal=1;
  617.     if (ri.RxOffset || width < 250 ) normal=0;
  618. }
  619.